#include "buzzer.h"
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/pwm.h"
#include "pico/util/queue.h"
#include "LOG.h"
#include <stdio.h>
#include "board.h"
#include "FreeRTOS.h"
#include "task.h"

// PWM相关变量
static uint8_t buzzer_gpio = BUZZER_DEFAULT_GPIO;
static uint16_t current_volume = 100; // 默认100%音量
static bool buzzer_initialized = false;
static uint slice_num;  // PWM切片号

// 蜂鸣器状态
static buzzer_state_t buzzer_state = BUZZER_STATE_IDLE;

// 任务相关
static volatile uint32_t buzzer_beep_duration = 0;

// 初始化PWM驱动
static bool buzzer_pwm_init(uint8_t gpio)
{
    LOG_INFO("buzzer_pwm_init - Initializing PWM on GPIO%d", gpio);
    
    // 保存GPIO引脚
    buzzer_gpio = gpio;
    
    // 初始化GPIO为PWM功能
    gpio_set_function(buzzer_gpio, GPIO_FUNC_PWM);
    
    // 获取PWM切片号和通道
    slice_num = pwm_gpio_to_slice_num(buzzer_gpio);
    uint channel = pwm_gpio_to_channel(buzzer_gpio);
    
    // 配置PWM以获得4kHz频率
    // 系统时钟为125MHz，要得到4kHz，需要设置合适的分频和计数上限
    pwm_config config = pwm_get_default_config();
    
    // 设置分频器，使时钟频率为250kHz (125MHz / 500 = 250kHz)
    // 这样计数器每4ms计数一次 (250kHz / 250 = 1kHz)
    pwm_config_set_clkdiv(&config, 500.0f);
    
    // 设置计数上限为62，这样PWM频率为 250kHz / 62 ≈ 4.032kHz
    pwm_config_set_wrap(&config, 62);
    
    // 初始化PWM
    pwm_init(slice_num, &config, true);
    
    // 设置初始占空比(50%)
    uint16_t level = 31; // 50%占空比 (62 / 2)
    pwm_set_chan_level(slice_num, channel, level);
    
    // 初始状态关闭PWM
    pwm_set_enabled(slice_num, false);
    gpio_put(buzzer_gpio, 0); // 确保GPIO为低电平
    
    LOG_INFO("  - PWM slice: %d, channel: %d, frequency: ~4.032kHz", slice_num, channel);
    LOG_INFO("buzzer_pwm_init completed successfully");
    return true;
}

// 蜂鸣器鸣响指定时间(毫秒)
void buzzer_beep(uint32_t duration_ms)
{
    if (!buzzer_initialized) {
        LOG_ERROR("Buzzer not initialized");
        return;
    }
    
    if (duration_ms == 0) {
        return;
    }
    
    LOG_INFO("Buzzer beep started for %d ms", duration_ms);
    
    // 设置蜂鸣时间，任务会自动处理
    buzzer_beep_duration = duration_ms;
}

// 蜂鸣器短促鸣响(默认时间)
void buzzer_beep_short(void)
{
    buzzer_beep(BUZZER_DEFAULT_DURATION);  // 默认100ms
}

// 停止蜂鸣器
void buzzer_stop(void)
{
    if (!buzzer_initialized) {
        LOG_WARN("Buzzer not initialized");
        return;
    }
    
    LOG_INFO("Stopping buzzer");
    
    // 清除蜂鸣时间
    buzzer_beep_duration = 0;
    
    // 立即停止PWM输出
    pwm_set_enabled(slice_num, false);
    gpio_put(buzzer_gpio, 0);  // 确保GPIO为低电平
    buzzer_state = BUZZER_STATE_IDLE;
}

// 设置蜂鸣器音量(0-100%)
void buzzer_set_volume(uint8_t volume_percent)
{
    if (!buzzer_initialized) {
        LOG_WARN("Buzzer not initialized");
        return;
    }
    
    if (volume_percent > 100) {
        volume_percent = 100;
    }
    
    current_volume = volume_percent;
    
    // 重新计算占空比(基于50%的基准上调整音量)
    uint channel = pwm_gpio_to_channel(buzzer_gpio);
    uint16_t level = (62 * current_volume) / 100 / 2;
    pwm_set_chan_level(slice_num, channel, level);
    
    LOG_DEBUG("Buzzer volume set to %d%%", volume_percent);
}

// 检查蜂鸣器是否正在鸣响
bool buzzer_is_beeping(void)
{
    return (buzzer_state == BUZZER_STATE_BEEPING);
}
